package com.example.envctel;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.util.Log;
import android.widget.RadioButton;
import android.widget.TextView;

import static android.os.SystemClock.sleep;

public class MyTcpService extends Service {

    private final String TAG = "MyTcpServer";
    Handler handler = new Handler();

    MainActivity mainActivity = MainActivity.getMainActivity();

    public MyTcpService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        // TODO: Return the communication channel to the service.
        throw new UnsupportedOperationException("Not yet implemented");
    }
    @Override
    public void onCreate()
    {
        super.onCreate();

        Log.i(TAG,"entry my tcp server");

        new Thread(new Runnable() {
            @Override
            public void run() {
                serialDeviceInit();
            }
        }).start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                serialHotplugStatusCheckInit();
            }
        }).start();

    }

    public int serialDeviceInit()
    {
        int channelnum = 0xff;

        /*启动串口服务 :确保串口初始化成功*/
        do {

                mainActivity.initModBusServerSuccessFlag =  mainActivity.environmentControl.initUsbSerialDeviceServer(0x00,channelnum,9600);

                sleep (10000);
                break;
        }while(true);

        System.out.printf("serial device init success");
        return 0x00;
    }

    public void serialHotplugStatusCheckInit()
    {
        int i = 0x00;
        int tmp = 0;

        TextView textView;
        int channelnum = 0xff;

        /*启动usb串口插拔检测服务*/
        do {
            mainActivity.initModBusServerSuccessFlag =  mainActivity.environmentControl.initUsbSerialHotplugServer();
            if (mainActivity.initModBusServerSuccessFlag == 0x00)
            {
                /*串口服务启动成功*/
                break;
            }
            sleep (10000);

        }while(true);

        /*处理usb串口设插拔之后的重新配置操作*/
        do
        {
            //for (i = 0x00; i < channelnum;i ++)
            {
                /*查询usb 串口状态*/
                mainActivity.usbSerialStatus = mainActivity.environmentControl.getUsbSerialStatus(channelnum);
                System.out.printf("usb serial state:"+ mainActivity.usbSerialStatus);

                tmp = mainActivity.usbSerialStatus;
                i = 0x00;

                /*串口初始化成功:设置已连接标识*/
                do{

                    tmp >>= (i<<1);

                    /*检测到已经连接上*/
                    if ((tmp & 0x00000003) != 0x00)
                    {
                        /*
                         * 更新串口连接状态
                         * */
                        switch(i)
                        {
                            case 0x00:
                            {
                                textView = (TextView)mainActivity.findViewById(R.id.textView40);
                                textView.setText("已连接");
                            }
                                break;
                            case 0x01:
                            {
                                textView = (TextView)mainActivity.findViewById(R.id.textView41);
                                textView.setText("已连接");
                            }
                                break;
                            default:
                                break;
                        }
                    }
                    else
                    {
                        /*
                         * 更新串口连接状态
                         * */
                        switch(i)
                        {
                            case 0x00:
                            {
                                textView = (TextView)mainActivity.findViewById(R.id.textView40);
                                textView.setText("未连接");
                            }
                            break;
                            case 0x01:
                            {
                                textView = (TextView)mainActivity.findViewById(R.id.textView41);
                                textView.setText("未连接");
                            }
                            break;
                            default:
                                break;
                        }
                    }
                }while((i ++) < 0x08);


                /*检测到usb串口，并且串口未进行初始化*/
                if ((mainActivity.usbSerialStatus == 0x01) ||(mainActivity.usbSerialStatus == 0x02) ||(mainActivity.usbSerialStatus == 0x04) ||(mainActivity.usbSerialStatus == 0x08) )
                {
                    //if (i == 0x00)
                    {
                        mainActivity.initModBusServerSuccessFlag =  mainActivity.environmentControl.initUsbSerialDeviceServer(0,channelnum,9600);
                    }
//                    else
//                    {
//                        mainActivity.initModBusServerSuccessFlag =  mainActivity.environmentControl.initUsbSerialDeviceServer(1,0x1,9600);
//                        mainActivity.initModBusServerSuccessFlag =  mainActivity.environmentControl.initUsbSerialDeviceServer(0x02,0x01,9600);
//                    }

                    if (mainActivity.initModBusServerSuccessFlag == 0x00)
                    {
                        System.out.println("usb serial init success");
                    }
                    else{
                        System.out.println("usb serial init fail");
                    }
                }
            }
            sleep(10000);

        }while(true);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
